package managedBean;

import Merlion.userManagement.bean.RegistrationBeanRemote;
import java.io.IOException;
import java.util.Properties;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

@ManagedBean(name = "mailbean")
@ViewScoped
public class MailBean {

    private String email;
    //@EJB
    //private CurrentSessionBeanRemote currentSessionBean;
    @EJB
    private RegistrationBeanRemote registrationBean;
    
    public MailBean() {
    }

  
    
    public void mailActivation(ActionEvent event) throws IOException {

        try {
           // email = currentSessionBean.getCurrentRegistrationId();
           
          FacesContext.getCurrentInstance().getExternalContext().getFlash().put("email", email);
            
            System.out.println("**MailBean: mailActivation()");

            Properties props = new Properties();
            System.out.println("**MailBean: connecting to Gmail Server...");
            props.put("mail.transport.protocol", "smtp");
            props.put("mail.smtp.host", "smtp.gmail.com");
            props.put("mail.smtp.port", "465");
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.socketFactory.port", "465");
            props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            props.put("mail.smtp.socketFactory.fallback", "false");

            System.out.println("**Mailbean: Authenticating sender...");

            javax.mail.Authenticator auth = new SMTPAuthenticator();
            Session session = Session.getInstance(props, auth);
            session.setDebug(true);
            System.out.println("**Mailbean: Setting up mail...");
            System.out.println(getEmail());
            Message msg = new MimeMessage(session);
            

            msg.setFrom(new InternetAddress("merlionHealthBar.com"));
            msg.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(getEmail())); //email address
            msg.setSubject("Account Activation");
            // msg.setContent("Dear"+email,"text/plain" );//+ email);
            msg.setContent("Dear " + getEmail() + "," + "</p>"
                    + "<font color=\"brown\" style=\"impact\"><h2>Welcome to Merlion Ltd</h2></font>"
                    + "<form><br/>Please <a href=\"https://localhost:8181/MerlionERP-war/userManagement/AccountActivation.xhtml\">click here</a> to activate your account "
                    +"and reset your password <a href=\"https://localhost:8181/MerlionERP-war/userManagement/ResetPassword.xhtml\">here</a> after activation</form><br/>"
                    +"Your default password is : 123456<br/>"
                    + "<p>Thank you for registering with Merlion Ltd",
                    "text/html");
            System.out.println("**MailBean: Sending mail...");
            Transport.send(msg);
            System.out.println("**MailBean: Verification email has been sent.");
            FacesContext.getCurrentInstance().getExternalContext().redirect("AdminDeptWorkspace.xhtml");

        } catch (MessagingException e) {
            System.out.println(e.getMessage());
        }
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}